package com.sp.business.normalization.impl;

import org.apache.commons.lang.NullArgumentException;

import com.sp.generated.xml.firemon.Database;
import com.sp.generated.xml.firemon.NetworkObjectCollection;
import com.sp.generated.xml.firemon.NormalizedDataSchema;
import com.sp.generated.xml.firemon.PolicyCollection;
import com.sp.generated.xml.firemon.RouteCollection;
import com.sp.generated.xml.firemon.RoutingInstanceCollection;
import com.sp.generated.xml.firemon.ServiceObjectCollection;
import com.sp.generated.xml.firemon.UserCollection;

public class DefaultKeyGenerator extends KeyGeneratorBase {
@Override
  public void generateKeys(NormalizedDataSchema previous, NormalizedDataSchema current)
  {
    if (current == null)
      throw new NullArgumentException("current");

    Database previousDatabase = null;
    UserCollection previousUsers = null;
    ServiceObjectCollection previousServices = null;
    NetworkObjectCollection previousNetworkObjects = null;
    PolicyCollection previousPolicies = null;
    RouteCollection previousRoutes = null;
    RoutingInstanceCollection previousRoutingInstances = null;

    if (previous != null)
    {
      previousDatabase = previous.getFireMon().getApplications().getFirewallApplication().getDatabase();
      previousUsers = previousDatabase.getUsers();
      previousServices = previousDatabase.getServiceObjects();
      previousNetworkObjects = previousDatabase.getNetworkObjects();
      previousPolicies = previousDatabase.getPolicies();
      previousRoutes = previousDatabase.getRoutes();
      previousRoutingInstances = previousDatabase.getRoutingInstances();
    }

    Database currentDatabase = current.getFireMon().getApplications().getFirewallApplication().getDatabase();

    UserKeyGenerator.generateUserKeys(previousUsers, currentDatabase.getUsers());
    ServiceKeyGenerator.generateServiceKeys(previousServices, currentDatabase.getServiceObjects());
    NetworkObjectKeyGenerator.generateNetworkObjectKeys(previousNetworkObjects, currentDatabase.getNetworkObjects());
    RouteKeyGenerator.generateRouteKeys(previousRoutes, currentDatabase.getRoutes());
    RoutingInstanceKeyGenerator.generateRoutingInstanceKeys(previousRoutingInstances, currentDatabase.getRoutingInstances());

    DefaultSecurityRuleKeyGenerator secRuleKeyGenerator = new DefaultSecurityRuleKeyGenerator();
    BaseNatRuleKeyGenerator natRuleKeyGenerator = new BaseNatRuleKeyGenerator();
    PolicyKeyGenerator policyKeyGenerator = new PolicyKeyGenerator(secRuleKeyGenerator, natRuleKeyGenerator);
    policyKeyGenerator.generatePolicyKeys(previousPolicies, currentDatabase.getPolicies());
  }
}
